<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>生态瞬时测试</title>
    <meta name="keywords" content="">
    <meta name="description" content="">

    <link rel="shortcut icon" href="favicon.ico">
    <link href="../css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="../css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="../css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="../css/animate.css" rel="stylesheet">
    <link href="../css/style.css?v=4.1.0" rel="stylesheet">
    <link rel="stylesheet" href="../css/paging.css">

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">

    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>实验管理</h5>
                </div>
                <div class="ibox-content">
                    <div class="row">
                        <div class="col-sm-5 m-b-xs">
                        </div>
                        <div class="col-sm-4 m-b-xs">
                        </div>
                        <div class="col-sm-3">
                            <a href="add-examination.html" class="btn btn-sm btn-primary">添加试卷</a>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead>
                            <tr>
                                <th>试卷名称</th>
                                <th>创建时间</th>
                                <th>创建者</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody id="test-box">
                            <!--表格加载内容显示-->
                            </tbody>
                        </table>
                    </div>
                    <div class="paging">
                        <div id="paging" class="page_div"></div>
                    </div>
                </div>

            </div>

        </div>

    </div>
</div>

<!-- 全局js -->
<script src="../js/jquery.min.js?v=2.1.4"></script>
<script src="../js/bootstrap.min.js?v=3.3.6"></script>
<script src="../js/paging.js"></script>
<script src="../js/dateChange.js"></script>
<!-- iCheck -->
<script src="../js/plugins/iCheck/icheck.min.js"></script>
<script>
    $(document).ready(function () {
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green',
        });
    });
</script>
<script>
    $(document).ready(function () {
        //获取页面数据
        function initData(_data) {
            var token = sessionStorage.getItem("token");
            var num = _data.current;
            var pageSize = _data.size;
            $.ajax({
                type:"GET",
                url:"http://123.206.18.189:8080/paper",
                data:_data,
                beforeSend: function(xhr){xhr.setRequestHeader('authToken', token);},
                dataType:'json',
                success: function (res) {
                    console.log(res);
                    var html = '';
                    for(var i=0; i<res.data.length;i++){
                        var day = dateChange(res.data[i].createTime);
                        html += `  <tr alt="${res.data[i].id}">
                                    <td>${res.data[i].name}</td>
                                    <td>${day}</td>
                                    <td>${res.data[i].createByUser}</td>
                                    <td>
                                        <!--<i class="fa fa-edit edit" title="修改" style="margin-right: 20px;"></i>-->
                                        <i class="fa fa-file-text exam-questions" title="试题管理" style="margin-right: 20px;"></i>
                                        <i class="fa fa-trash-o del" title="删除试卷" style="margin-right: 20px;"></i>
                                    </td>
                                </tr>`
                    }
                    $('#test-box').html(html);
                    updatePage(res,_data);
                },
                error:function(XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.status);
                    console.log(XMLHttpRequest.readyState);
                    console.log(textStatus);
                },

            });
        }
        initData({curPage:1,pageSize:10});
        //分页效果
        function updatePage(res,_data) {
            $("#paging").paging({
                pageNo:_data.curPage,
                totalPage:Math.ceil(res.totalRecord/_data.pageSize),
                callback: function (num) {
                    _data.curPage = num;
                    initData(_data)
                }
            })
        }
        //删除实验
        $("#test-box").on("click",".del",function () {
            var token = sessionStorage.getItem("token");
            var $testId = $(this).parent().parent().attr("alt");
            var r = confirm("您是否确定删除该试卷？");
            if(r){
                $.ajax({
                    type:"DELETE",
                    url:"http://123.206.18.189:8080/paper/"+$testId,
                    headers:{
                        authToken:token
                    },
                    success:function () {

                    },
                    complete:function (XMLHttpRequest) {
                        if(XMLHttpRequest.status == 200){
                            alert("试卷删除成功！");
                            window.location.reload();
                        }
                    }
                })
            }

        })
        //跳转试题管理
        $("#test-box").on("click",".exam-questions",function () {
            var examId = $(this).parent().parent().attr("alt");
            var examName = $(this).parent().parent().children().eq(0).text();
            sessionStorage.setItem("examName",examName);
            sessionStorage.setItem("examId",examId);
            window.location.href = "exam-questions.html";

        })
    })
</script>

</body>

</html>
